package com.mxf.code.iaop.accountlog;

import com.alibaba.fastjson.JSON;
import com.mxf.code.entity.LogInfo;
import com.mxf.code.service.CallbackService;
import com.mxf.code.service.LogInfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author : [mxf]
 * @version : [v1.0]
 * @className : WebLogAspect
 * @description : [描述说明该类的功能]
 * @createTime : [2022/9/10 22:47]
 * @updateUser : [mxf]
 * @updateTime : [2022/9/10 22:47]
 * @updateRemark : [描述说明本次修改内容]
 */
@Aspect
@Component
@Order(2)
@Slf4j
public class AccountLogAspect {

    @Pointcut("execution(public * com.mxf.code.service.AccountService.*(..))")
    private void accountLog(){}

    @Autowired
    private LogInfoService logInfoService;

    @Autowired
    private CallbackService callbackService;
    

    @After(value = "accountLog()")
    public void doAfter(JoinPoint joinPoint) throws Throwable {
        callbackService.doCallback("调用After");
        String s = JSON.toJSONString(joinPoint.getArgs());
        LogInfo logInfo = new LogInfo();
        logInfo.setParams(s);
        logInfo.setMsg(joinPoint.toString());
        logInfoService.save(logInfo);
    }

    @AfterReturning(value = "accountLog()", returning = "ret")
    public void doAfterReturning(JoinPoint joinPoint,Object ret) throws Throwable {
        callbackService.doCallback("调用AfterReturning");
        String s = JSON.toJSONString(joinPoint.getArgs());
        LogInfo logInfo = new LogInfo();
        logInfo.setParams(s);
        logInfo.setMsg(joinPoint.toString());
        logInfo.setStatus("0");
        logInfoService.save(logInfo);
    }

    @AfterThrowing(value = "accountLog()", throwing = "e")
    public void doAfterReturning(JoinPoint joinPoint,Throwable e) throws Throwable {
        callbackService.doCallback("调用AfterThrowing");
        String s = JSON.toJSONString(joinPoint.getArgs());
        LogInfo logInfo = new LogInfo();
        logInfo.setParams(s);
        logInfo.setMsg(joinPoint.toString());
        logInfo.setErrorInfo(e.getMessage());
        logInfo.setStatus("1");
        logInfoService.save(logInfo);

    }

}
